const path = require("path");
const {profilerUseTime, notice, validConfig} = require("./utils/util");
const fileSearch = require("./utils/fileSearch");
const dispatcher = require("./processManage/mast_worker");
const logger = require("./logger");
const rootPath = require("./utils/rootPath");

function run() {
  const options = require(path.join(rootPath, './config/', process.env.config + '.config.js'));

  profilerUseTime();
  notice('正在加载配置.');

  validConfig(options)

  let files = [];
  if (options?.files) {
    files = options.files;
  } else if (options?.includes) {
    notice('正在检索文件夹.');
    files = fileSearch(options)
  }

  if(options?.filterFiles){
    files = options.filterFiles(files);
  }

  if (!files.length) {
    notice('没有需要处理的文件!!!', 'warn');
    process.exit();
  }

  notice(`共找到${files.length}份匹配文件.`);
  global.__shouldCheckFileCount = files.length;

  if (options?.logger) {
    const logConfig = options.logger;
    logger.init();

    if (logConfig.types && Object.keys(logConfig.types)?.length) {
      logger.register(logConfig.types);
    }
  }
  dispatcher(options, files);
}

module.exports = run;
